<?php

namespace System\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Session\Container;
use System\Model\Utility;
use System\Model\Catalog;

class CatalogController extends AbstractActionController {

    private $CatalogTable;

    private function getCatalogTable() {
        if (!$this->CatalogTable) {
            $pst = $this->getServiceLocator();
            $this->CatalogTable = $pst->get("System\Model\CatalogTable");
        }
        return $this->CatalogTable;
    }

    private function setLayout() {
        $this->layout("layout/layoutadmin");
    }

    private function checkLogin() {
        $session = new Container("ssadmin");
        if ($session->adminusername == null) {
            $this->redirect()->toUrl(WEB_PATH . "/cpadmin");
        }
    }

    public function indexAction() {
        $this->checkLogin();
        $this->setLayout();
        $listcatalog = $this->getCatalogTable()->loadcatalog(-1);
        return array("listcatalog" => $listcatalog);
    }

    public function loadcatalogAction() {
        $parentId = $this->params()->fromPost("parentId");
        $listcatalog = $this->getCatalogTable()->loadcatalog($parentId);
        $view = "";
        $index = 1;
        $objul = new Utility();
        if (count($listcatalog) < 1) {
            $view.="Chưa có chuyên mục nào.";
        } else {
            foreach ($listcatalog as $key => $listdata) {
                $status = '<span class="label label-success">Đang chạy</span>';
                if ($listdata["catalogActive"] == false) {
                    $status = '<span class="label label-danger">Tạm ngưng</span>';
                }
                $view .='<tr id="' . $listdata["catalogId"] . '">' .
                        '<td>' . $index . '</td>' .
                        '<td>' . $listdata["catalogName"] . '</td>' .
                        '<td>' . $listdata["catalogDes"] . '</td>' .
                        '<td>' . $objul->formatDate($listdata["catalogDate"]) . '</td>' .
                        '<td>' . $objul->formatDate($listdata["catalogUpdate"]) . '</td>';
                if ($listdata["showindex"] ==true) {
                    $view .='<td><a href="javascript:void(0);" onclick="showindex(' . $listdata["catalogId"] . ', 0);"><i class="fa fa-check-circle-o" style="color:#5CB85C;"></i> Hiển thị</a></td>';
                } else {
                    $view .='<td><a href="javascript:void(0);" onclick="showindex(' . $listdata["catalogId"] . ', 1);"><i class="fa fa-check-circle-o" style="color:#900;"></i> None</a></td>';
                }
                $view .='<td>' . $status . '</td>' .
                        '<td>' .
                        '<a onclick="delcatalog(' . $listdata["catalogId"] . ');" style="padding:0px 5px;background: #990000;color:#FFF;border-radius: 3px;" href="javascript:void(0);" title=""><i class="fa fa-trash-o"></i></a>' .
                        '<a onclick="modalchange(' . $listdata["catalogId"] . ');" style="padding:0px 5px;background: #ff9933;color:#FFF;border-radius: 3px;" href="javascript:void(0);" title=""><i class="fa fa-pencil-square-o"></i></a>' .
                        '<a onclick="modal(' . $listdata["catalogId"] . ',' . $listdata["catalogParent"] . ');" style="padding:0px 5px;background: #5CB85C;color:#FFF;border-radius: 3px;" href="javascript:void(0);" title=""><i class="fa fa-retweet"></i></a>';
                if ($listdata["catalogActive"] == false) {
                    $view.='<a onclick="setActive(' . $listdata["catalogId"] . ',1);" style="padding:0px 5px;background: #006600;color:#FFF;border-radius: 3px;" href="javascript:void(0);" title=""><i class="fa fa-unlock-alt"></i></a>';
                } else {
                    $view .='<a onclick="setActive(' . $listdata["catalogId"] . ',0);" style="padding:0px 5px;background: #990000;color:#FFF;border-radius: 3px;" href="javascript:void(0);" title=""><i class="fa fa-lock"></i></a>';
                }

                $view .='</td>' .
                        '</tr>';
                $index ++;
            }
        }
        echo $view;
        die;
    }

    public function showindexAction() {
        $catalogId = addslashes(trim($this->params()->fromPost("catalogId")));
        $flag = addslashes(trim($this->params()->fromPost("flag")));
        $this->getCatalogTable()->showindex($catalogId, $flag);
        die;
    }

    public function addcatalogAction() {
        $objUl = new Utility();
        $objCatalog = new Catalog();
        $cataName = $objUl->removescript(addslashes(trim($this->params()->fromPost("cataname"))));
        if ($cataName == null) {
            echo "Bạn chưa nhập tên chuyên mục.";
        } else {
            $cataDes = $objUl->removescript(addslashes(trim($this->params()->fromPost("catades"))));
            $parentId = addslashes(trim($this->params()->fromPost("parentId")));
            $catalogLink = $objUl->chuyenDoi($cataName);
            $catalogSort = 0;
            $catalogDate = Date("Y:m:d");
            $catalogUpdate = Date("Y:m:d");
            $catalogActive = true;
            $data = array();
            $data["catalogName"] = $cataName;
            $data["catalogDes"] = $cataDes;
            $data["catalogParent"] = $parentId;
            $data["catalogLink"] = $catalogLink;
            $data["catalogDate"] = $catalogDate;
            $data["catalogUpdate"] = $catalogUpdate;
            $data["catalogActive"] = $catalogActive;
            $data["catalogSort"] = $catalogSort;
            $objCatalog->exchangeArray($data);
            $checkadd = $this->getCatalogTable()->addCatalog($objCatalog);
            if ($checkadd) {
                echo "";
            } else {
                echo "Hệ thống đang bận.Bạn vui lòng thử lại sau!";
            }
        }
        die;
    }

    public function delcataAction() {
        $cateId = addslashes(trim($this->params()->fromPost("catalogId")));
        $cataRoot = $cateId;
        $data = array();
        $abc = $this->getCatalogTable()->scanlistparentcata($cataRoot);
        $data = explode("-", $abc);
        $data2 = array_unique($data);
        if ($data2[0] == null) {
            $data2[0] = $cataRoot;
        }
        $checkvides = $this->getCatalogTable()->checkdelvideos($data2);
        if ($checkvides == "") {
            $checkdel = $this->getCatalogTable()->delcata($data2);
            if ($checkdel) {
                echo "Xóa dữ liệu thành công.*duypc*delok";
            } else {
                echo "Máy chủ đang bận bạn vui lòng thử lại sau.*duypc*delnotok";
            }
        } else {

            $this->getCatalogTable()->delImgVideos($checkvides);
            $this->getCatalogTable()->delvideos($data2);
            $checkdel = $this->getCatalogTable()->delcata($data2);
            if ($checkdel) {
                echo "Xóa dữ liệu thành công.*duypc*delok";
            } else {
                echo "Máy chủ đang bận bạn vui lòng thử lại sau.*duypc*delnotok";
            }
        }
        die;
    }

    public function activeAction() {
        $catalogId = addslashes(trim($this->params()->fromPost("catalogId")));
        $catalogActive = addslashes(trim($this->params()->fromPost("active")));
        $objCatalog = new Catalog();
        $objCatalog->setCataId($catalogId);
        $objCatalog->setActive($catalogActive);
        $this->getCatalogTable()->updateActive($objCatalog);
        die;
    }

    public function changeparentAction() {
        $catalogId = addslashes(trim($this->params()->fromPost("catalogId")));
        $catalogparentId = addslashes(trim($this->params()->fromPost("parentId")));
        $objCatalog = new Catalog();
        $objCatalog->setCataId($catalogId);
        $objCatalog->setCataParent($catalogparentId);
        $this->getCatalogTable()->updateparent($objCatalog, $catalogparentId);

        die;
    }

    public function loadeditAction() {
        $catalogId = addslashes(trim($this->params()->fromPost("catalogId")));
        $data = $this->getCatalogTable()->loaddataedit($catalogId);
        echo $data["catalogName"] . "*duypc*" . $data["catalogDes"];
        die;
    }

    public function editAction() {
        $objUl = new Utility();
        $objCatalog = new Catalog();
        $catalogId = $objUl->removescript(addslashes(trim($this->params()->fromPost("catalogId"))));
        $cataName = $objUl->removescript(addslashes(trim($this->params()->fromPost("cataname"))));
        if ($cataName == null) {
            echo "Bạn chưa nhập tên chuyên mục.";
        } else {
            $cataDes = $objUl->removescript(addslashes(trim($this->params()->fromPost("catades"))));
            $catalogLink = $objUl->chuyenDoi($cataName);
            $catalogUpdate = Date("Y:m:d");
            $data = array();
            $data["catalogName"] = $cataName;
            $data["catalogDes"] = $cataDes;
            $data["catalogLink"] = $catalogLink;
            $data["catalogUpdate"] = $catalogUpdate;
            $objCatalog->exchangeArray($data);
            $checkUpdate = $this->getCatalogTable()->updateCatalog($objCatalog, $catalogId);
            if ($checkUpdate) {
                echo "";
            } else {
                echo "Hệ thống đang bận.Bạn vui lòng thử lại sau!";
            }
        }
        die;
    }

    public function testAction() {
        $pa = $this->params()->fromQuery("page");
        echo $pa;
    }

}
